/*
 * @FileName: [MybatisPlusConfig4ds2.java] 
 * @Package top.zyblogs.datasourcedemo 
 * @author zhaoyibo
*/


package top.zyblogs.datasourcedemo;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;

/**
 * <strong>Description : </strong>
 * <br>
 * <strong>Create on : 2020年4月21日 下午6:37:23<br></strong>
 * <p>
 * <strong>Copyright (C) Inspur Co.,Ltd.<br></strong>
 * <p>
 * @author zhaoyibo
 * @version <strong>V1.0</strong>
 */
@Configuration
@MapperScan(basePackages ="top.zyblogs.datasourcedemo.ds2.**.dao", sqlSessionTemplateRef  = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2 {

   //ds2数据源
   @Bean("ds2SqlSessionFactory")
   public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
       MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
       sqlSessionFactory.setDataSource(dataSource);
       sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
               getResources("classpath*:com/web/ds2/**/*.xml"));
       return sqlSessionFactory.getObject();
   }

//事务支持
   @Bean(name = "ds2TransactionManager")
   public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
       return new DataSourceTransactionManager(dataSource);
   }

   @Bean(name = "ds2SqlSessionTemplate")
   public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
       return new SqlSessionTemplate(sqlSessionFactory);
   }

}